const localStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')

function initialize(passport, getUserByEmail, getUserById) {
	const authenticateUser = async (email, password, done) => {
		const user = getUserByEmail(email)
		if (user == null) {
			return done(null, false, {
				message: 'Пользователь с таким email не найден!',
			})
		}
		try {
			if (await bcrypt.compare(password, user.password)) {
				return done(null, user)
			} else {
				return done(null, false, { message: 'Неверный пароль!' })
			}
		} catch (e) {
			console.log(e)
			return done(e)
		}
	}
	passport.use(new localStrategy({ usernameField: 'email' }, authenticateUser))
	passport.serializeUser((user, done) => done(null, user.id))
	passport.deserializeUser((id, done) => {
		return done(null, getUserById(id))
	})
}

module.exports = initialize
